package pl.edu.mimuw.irs.rcp.controller.editor.workflow.part;

import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;

import pl.edu.mimuw.irs.core.model.design.net.IRSTransition;
import pl.edu.mimuw.irs.core.util.Converter;
import pl.edu.mimuw.irs.rcp.util.MessageCode;
import pl.edu.mimuw.irs.rcp.util.UITexts;


/**
 * @author Jakub Rauch
 * Created on: 2009-05-16
 */
public class IRSTransitionEditPart extends IRSAbstractNodeEditPart {
	
	// *** Attributes
	
	protected TransitionPropertySource propertySource;
	
	public TransitionPropertySource getPropertySource() {
		if (propertySource == null) {
			propertySource = new TransitionPropertySource(
					Converter.asIRSTransition(getIRSNode()));
		}
		return propertySource;
	}
	
	// *** Methods

	protected NodeShape getNodeShape() {
		return NodeShape.IRSTRANSITION;
	}
	
	// *** Inner classes
	
	public class TransitionPropertySource extends NodePropertySource {

		protected static final String PROPERTY_CHANCE = "properties.transition.chance";
		
		// *** Attributes
		
		private IRSTransition transition;
		public IRSTransition getTransition() {
			return transition;
		}
		
		private IPropertyDescriptor[] propertyDescriptors;
		
		private double orgChance;
		
		// *** Constructors
		
		public TransitionPropertySource(IRSTransition transition) {
			super(transition);
			this.transition = transition;
			this.orgChance = transition.getChance();
		}
		
		// *** Methods
		
		@Override
		public IPropertyDescriptor[] getPropertyDescriptors() {
			if (propertyDescriptors == null) {
				IPropertyDescriptor[] superPropertyDescriptors = super.getPropertyDescriptors();
				
	            // Create a descriptors and set a category
	            PropertyDescriptor chanceDescriptor = 
	            	new TextPropertyDescriptor(PROPERTY_CHANCE, 
	            			UITexts.get(MessageCode.WORKFLOW_EDITOR_TRANSITION_CHANCE));
	            chanceDescriptor.setCategory(UITexts.get(MessageCode.PROPERTIES_CATEGORY_BEHAVIOR));

	            propertyDescriptors = 
	            	new IPropertyDescriptor[superPropertyDescriptors.length + 1];
	            
	            System.arraycopy(superPropertyDescriptors, 0, 
	            		propertyDescriptors, 0, superPropertyDescriptors.length);
	            
	            propertyDescriptors[superPropertyDescriptors.length] = chanceDescriptor;
            }
            return propertyDescriptors;
		}

		@Override
		public Object getPropertyValue(Object id) {
			if (PROPERTY_CHANCE.equals(id)) {
				return String.valueOf(transition.getChance());
			}
			return super.getPropertyValue(id);
		}

		@Override
		public boolean isPropertySet(Object id) {
			if (PROPERTY_CHANCE.equals(id)) {
	        	String curValue = (String)getPropertyValue(id);
				return !curValue.equals(String.valueOf(this.orgChance));
			}
			return super.isPropertySet(id);
		}

		@Override
		public void resetPropertyValue(Object id) {
			if (PROPERTY_CHANCE.equals(id)) {
				setPropertyValue(id, String.valueOf(this.orgChance));
			}
			super.resetPropertyValue(id);
		}

		@Override
		public void setPropertyValue(Object id, Object value) {
			if (PROPERTY_CHANCE.equals(id)) {
				transition.setChance(Double.valueOf((String)value)); // XXX : catch
			}
			super.setPropertyValue(id, value);
		}
	}
}
